define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'entrepot/receiving/index' + location.search,
                    add_url: 'entrepot/receiving/add',
                    edit_url: 'entrepot/receiving/edit',
                    del_url: 'entrepot/receiving/del',
                    multi_url: 'entrepot/receiving/multi',
                    import_url: 'entrepot/receiving/import',
                    settle_url: 'entrepot/receiving/settle',
                    table: 'receiving',
                },
                pageSize: 15,
                pageList: [15],
            });
            //退料
            $(".btn-settle").click(function(){
                var temp=table.bootstrapTable('getSelections');
                let ids = '';
                var arr=[];
                temp.map(item=>{
                    arr.push(item.id);
                })
                ids = arr.join(',');
                layer.confirm('确定结算？', {
                }, function(index, layero){
                    Fast.api.ajax({
                        url: 'entrepot/receiving/settle',
                        type: 'post',
                        data:{ids:ids},
                    }, function (data, ret) {
                        layer.close(index);
                    });
                    layer.close(index);
                });

            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                dblClickToEdit:false,
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {field: 'createtime', title: __('收货时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'order.order_no', title: __('Order.order_no'), operate: 'LIKE'},
                        {field: 'detail.trademark_printing', title: __('Detail.trademark_printing'), operate: 'LIKE'},
                        {field: 'detail.abbreviation', title: __('Detail.abbreviation'), operate: 'LIKE'},
                        {field: 'colour.name', title: __('Colour.name'), operate: 'LIKE'},
                        {field: 'detail.quantity', title: __('Detail.quantity')},
                        {field: 'employee.name', title: __('Employee.name'), operate: 'LIKE'},
                        {field: 'admin.nickname', title: __('收货管理员'), operate: 'LIKE'},
                        {field: 'attribute.name', title: __('Attribute.name'), operate: 'LIKE'},
                        // {field: 'ddd', title: __('工序累计收取量'), operate: 'LIKE'},
                        {field: 'quantity', title: __('Quantity')},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'),table: table,  events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'edit',
                                    text: '编辑',
                                    title: '编辑',
                                    icon: 'fa fa-pencil',
                                    classname: 'btn btn-xs btn-success btn-editone btn-dialog',
                                    url: 'options.extend.edit_url',
                                    disable: function (row) {
                                        var val=$('#edit-type').val();
                                        console.log('DD',row.status,typeof row.status)
                                        if(row.group_id == 1 || row.group_id == 2){
                                            return false;
                                        }else{
                                            // if(row.status==1 && !val){
                                            if(row.status==1){
                                                return true;
                                            }else{
                                                return false;
                                            }
                                        }

                                        console.log('DD',row.group_id,typeof row.group_id)
                                        // if(row.status=='1' && !val){
                                        //     if(row.group_id == 5){
                                        //         return false;
                                        //     }else{
                                        //         return true;
                                        //     }
                                        // }else{
                                        //     return false;
                                        // }
                                    }
                                }
                            ],
                        },

                        // {field: 'quantity', title: __('Quantity')},
                        // {field: 'quantity_count', title: __('Quantity_count')},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        // {field: 'order.order_no', title: __('Order.order_no'), operate: 'LIKE'},
                        // {field: 'detail.trademark_printing', title: __('Detail.trademark_printing'), operate: 'LIKE'},
                        // {field: 'detail.abbreviation', title: __('Detail.abbreviation'), operate: 'LIKE'},
                        // {field: 'detail.quantity', title: __('Detail.quantity')},
                        // {field: 'detail.cumulative_quantity', title: __('Detail.cumulative_quantity')},
                        // {field: 'employee.name', title: __('Employee.name'), operate: 'LIKE'},
                        // {field: 'attribute.name', title: __('Attribute.name'), operate: 'LIKE'},
                        // {field: 'colour.name', title: __('Colour.name'), operate: 'LIKE'},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                onCheck:function(row,$element){
                    if(row.status==1){
                        $('.btn-edit').attr('disabled', true);
                    }else{
                        $('.btn-edit').attr('disabled', false);
                    }
                },
            });


            // 为表格绑定事件
            Table.api.bindevent(table);


        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'entrepot/receiving/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'entrepot/receiving/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'entrepot/receiving/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        //点击行前面的选择框

        add: function () {
            $.validator.config({
                rules: {
                    is_over: function (element) {
                        var $parent=$(element).parents('.form-inline');
                        var order_quantity=$parent.children('ins').eq(1).find('input').val();
                        var process_received=$parent.children('ins').eq(2).find('input').val();
                        var total=Number(process_received)+Number(element.value);
                        if (total>Number(order_quantity)) {
                            return '总和不能超过'+order_quantity;
                        }
                    }
                }
            });

            $('#c-order_id').on('change',function () {
                $("#c-trademark_printing").selectPageClear();
                $("#c-colour_id").selectPageClear();
            });

            $("#c-trademark_printing").data("params", function(){
                const order_id  = $("input[name='row[order_id]']").val();
                return {custom: {order_id:order_id}};

            });

            $('#c-trademark_printing').on('change',function () {
                $("#c-colour_id").selectPageClear();
                const order_id  = $("input[name='row[order_id]']").val();
                const trademark_printing  = $("input[name='row[trademark_printing]']").val();
                // 标识
                $.ajax({
                        url: 'entrepot/receiving/get_abbreviation',
                        type: 'POST',
                        data: {order_id:order_id,trademark_printing:trademark_printing},
                        dataType: 'json',
                        success: function (result) {
                            $('#c-abbreviation').val(result);
                        }
                });


            });

            $("#c-colour_id").data("params", function(){
                const order_id  = $("input[name='row[order_id]']").val();
                const trademark_printing  = $("input[name='row[trademark_printing]']").val();
                return {custom: {order_id:order_id,trademark_printing:trademark_printing}};
            });

            $('#c-colour_id').on('change',function () {
                $("#c-process_attribute_id").selectPageClear();

            });

            $("#c-process_attribute_id").data("params", function(){
                const order_id  = $("input[name='row[order_id]']").val();
                const trademark_printing  = $("input[name='row[trademark_printing]']").val();
                const colour_id  = $("input[name='row[colour_id]']").val();
                return {custom: {order_id:order_id,trademark_printing:trademark_printing,colour_id:colour_id}};
            });
            $('#c-process_attribute_id').on('change',function () {
                const order_id  = $("input[name='row[order_id]']").val();
                const trademark_printing  = $("input[name='row[trademark_printing]']").val();
                const colour_id  = $("input[name='row[colour_id]']").val();
                const process_attribute_id  = $("input[name='row[process_attribute_id]']").val();
                if(process_attribute_id){
                    Fast.api.ajax({
                        url: 'entrepot/receiving/get_list',
                        type: 'post',
                        data:{order_id:order_id,trademark_printing:trademark_printing,colour_id:colour_id,process_attribute_id:process_attribute_id},
                    }, function (data, ret) {
                        $("#c-process_detail").val(data);
                        $("#c-process_detail").trigger("fa.event.refreshfieldlist");
                        Form.events.selectpage($("#j-process"));
                    });
                }
                // Form.events.selectpage($("#j-process"));
            });
            //收取详情

            // $(document).on("fa.event.appendfieldlist", ".btn-append", function(){
            //     Form.events.selectpage($("#j-process"));
            // });

            Controller.api.bindevent();
        },
        edit: function () {

            Controller.api.bindevent();

        },
        settle: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },

    };

    return Controller;
});
